package com.m.demo.mapper;

import com.m.demo.model.UserInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserInfo2Mapper {
    @Insert("<script>" +
            "insert into user_info (username, password, age," +
            "<if test='gender!=null'>" +
            "gender,</if>" +
            "phone)" +
            "values (#{username}, ${password}, #{age}," +
            "<if test='gender!=null'> #{gender},</if>" +
            "#{phone})" +
            "</script>")
    Integer insert(UserInfo userInfo);

    Integer insertByXML(UserInfo userInfo);

    /**
     * 使用where标签
     */
    List<UserInfo> selectByCondition(UserInfo userInfo);

    /**
     * 使用trim标签
     */
    List<UserInfo> selectByCondition2(UserInfo userInfo);

    /**
     * 使用注解
     */
    @Select("<script>" +
            "select * from user_info" +
            "        <where>" +
            "            <if test=\"username!=null\">" +
            "                username = #{username}" +
            "            </if>" +
            "            <if test=\"age!=null\">" +
            "                and age = #{age}" +
            "            </if>" +
            "            <if test=\"gender!=null\">" +
            "                and gender = #{gender}" +
            "            </if>" +
            "        </where>" +
            "</script>")
    List<UserInfo> selectByCondition3(UserInfo userInfo);

    /**
     * 使用trim标签
     */
    Integer updateByCondition(UserInfo userInfo);

    /**
     * 使用set标签
     */
    Integer updateByCondition2(UserInfo userInfo);

    Integer batchDelete(@Param("ids")List<Integer> ids);
}
